using Vin.Win32.Input.Enum;
using Win32;

namespace Vin.Win32.Input;

public class VinInputBuilder
{
    private readonly List<INPUT> _inputs;

    public VinInputBuilder()
    {
        _inputs = new List<INPUT>();
    }

    /// <summary>
    /// 返回数组
    /// </summary>
    /// <returns></returns>
    public INPUT[] ToArray()
    {
        return _inputs.ToArray();
    }

    /// <summary>
    /// 判断是否是扩展键
    /// </summary>
    /// <param name="keyCode"></param>
    /// <returns></returns>
    public bool IsExtendedKey(VinVirtualKeyCode keyCode)
    {
        if (keyCode == VinVirtualKeyCode.MENU ||
            keyCode == VinVirtualKeyCode.LMENU ||
            keyCode == VinVirtualKeyCode.RMENU ||
            keyCode == VinVirtualKeyCode.CONTROL ||
            keyCode == VinVirtualKeyCode.RCONTROL ||
            keyCode == VinVirtualKeyCode.INSERT ||
            keyCode == VinVirtualKeyCode.DELETE ||
            keyCode == VinVirtualKeyCode.HOME ||
            keyCode == VinVirtualKeyCode.END ||
            keyCode == VinVirtualKeyCode.PRIOR ||
            keyCode == VinVirtualKeyCode.NEXT ||
            keyCode == VinVirtualKeyCode.RIGHT ||
            keyCode == VinVirtualKeyCode.UP ||
            keyCode == VinVirtualKeyCode.LEFT ||
            keyCode == VinVirtualKeyCode.DOWN ||
            keyCode == VinVirtualKeyCode.NUMLOCK ||
            keyCode == VinVirtualKeyCode.CANCEL ||
            keyCode == VinVirtualKeyCode.SNAPSHOT ||
            keyCode == VinVirtualKeyCode.DIVIDE)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /// <summary>
    /// 添加按键按下
    /// </summary>
    public VinInputBuilder AddKeyDown(VinVirtualKeyCode keyCode)
    {
        _inputs.Add(INPUT.KeyboardInput(new KEYBDINPUT()
        {
            wVk = (ushort)keyCode,
            dwFlags = IsExtendedKey(keyCode) ? KeyEventFlags.KEYEVENTF_EXTENDEDKEY : 0,
            time = 0,
            dwExtraInfo = IntPtr.Zero
        }));
        return this;
    }

    /// <summary>
    /// 添加按键抬起
    /// </summary>
    public VinInputBuilder AddKeyUp(VinVirtualKeyCode keyCode)
    {
        _inputs.Add(INPUT.KeyboardInput(new KEYBDINPUT()
        {
            wVk = (ushort)keyCode,
            dwFlags = IsExtendedKey(keyCode)
                ? KeyEventFlags.KEYEVENTF_KEYUP | KeyEventFlags.KEYEVENTF_EXTENDEDKEY
                : KeyEventFlags.KEYEVENTF_KEYUP,
            time = 0,
            dwExtraInfo = IntPtr.Zero
        }));
        return this;
    }

    /// <summary>
    /// 添加按按键
    /// </summary>
    public VinInputBuilder AddKeyPress(VinVirtualKeyCode keyCode)
    {
        AddKeyDown(keyCode);
        AddKeyUp(keyCode);
        return this;
    }

    /// <summary>
    /// 文本输入
    /// </summary>
    public VinInputBuilder TextEntry(string msg)
    {
        foreach (var s in msg)
        {
            _inputs.Add(INPUT.KeyboardInput(new KEYBDINPUT()
            {
                wVk = 0,
                dwFlags = KeyEventFlags.KEYEVENTF_UNICODE,
                dwExtraInfo = IntPtr.Zero,
                wScan = s,
                time = 0
            }));
        }

        return this;
    }
}